package com.base.cn.platform.os.controller.web.global;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.service.course.cou.CourseService;
import com.base.cn.platform.os.service.exam.paper.ExamPaperService;
import com.base.cn.platform.os.service.manage.op.article.OpArticleService;
import com.base.cn.platform.os.service.manage.teacher.SysTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 全局搜索结果
 */
@Controller
@RequestMapping("/web/globalSearch")
public class WebGlobalSearchController extends WebBaseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private SysTeacherService sysTeacherService;
    @Autowired
    private OpArticleService articleService;
    @Autowired
    private ExamPaperService examPaperService;

    /**
     * 全局搜索结果
     *
     * @param model
     * @param overallValue
     * @return
     */
    @RequestMapping("/globalSearchIndex")
    public String globalSearchIndex(Model model, @RequestParam(value = "overallName",required = false) String overallName,@RequestParam(value = "overallValue",required = false) String[] overallValue) {
        StringBuffer overallValueString = new StringBuffer();
        if(ObjectUtils.isNotEmpty(overallValue)){
            for (String o : overallValue) {
                overallValueString.append(o).append(",");
            }
            model.addAttribute("overallValue", overallValueString.substring(0, overallValueString.length() - 1));
        }
        model.addAttribute("overallName",overallName);
        return resultPage("/globalSearch/global-search");
    }

    /**
     * 获取查询的课程数据
     *
     * @param model
     * @param overallValue
     * @return
     */
    @RequestMapping("/globalSearchCourse")
    public String globalSearchCourse(Model model, @RequestParam("overallValue") String overallValue) {
        Map<String, Object> condition = new HashMap<>();
        condition.put("courseNames", overallValue);
        List<Map<String, Object>> dataList = new ArrayList<>();
        if(StringUtils.isNotEmpty(overallValue)){
            dataList = courseService.findCourseList(condition, false, false, false, true);
        }
        model.addAttribute("dataList", dataList);
        return resultPage("/globalSearch/groom-search-course-list");
    }

    /**
     * 获取查询的教师数据
     * @param model
     * @param overallValue
     * @return
     */
    @RequestMapping("/globalSearchTeacher")
    public String globalSearchTeacher(Model model, @RequestParam("overallValue") String overallValue) {
        Map<String, Object> condition = new HashMap<>();
        condition.put("teacherNames", overallValue);
        List<Map<String, Object>> dataList = new ArrayList<>();
        if(StringUtils.isNotEmpty(overallValue)) {
            dataList = sysTeacherService.findSysTeacherList(condition, false, false, false, false, false);
        }
        model.addAttribute("dataList", dataList);
        return resultPage("/globalSearch/groom-search-teacher-list");
    }

    /**
     * 获取查询的试卷数据
     * @param model
     * @param overallValue
     * @return
     */
    @RequestMapping("/globalSearchExamPaper")
    public String globalSearchQuestion(HttpServletRequest request,Model model, @RequestParam("overallValue") String overallValue) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> params = new HashMap<>();
        params.put("paperNames", overallValue);
        params.put("userId",userId);
        params.put("examBuyStatusData",true);
        params.put("status",1);
        params.put("useType",1);
        List<Map<String, Object>> dataList = new ArrayList<>();
        if(StringUtils.isNotEmpty(overallValue)) {
            dataList = examPaperService.findExamPaperList(params);
        }
        model.addAttribute("dataList", dataList);
        return resultPage("/globalSearch/groom-search-examPaper-list");
    }

    /**
     * 获取查询的咨询数据
     * @param model
     * @param overallValue
     * @return
     */
    @RequestMapping("/globalSearchArticle")
    public String globalSearchArticle(Model model, @RequestParam("overallValue") String overallValue) {
        Map<String, Object> params = new HashMap<>();
        params.put("articleNames", overallValue);
        List<Map<String, Object>> dataList = new ArrayList<>();
        if(StringUtils.isNotEmpty(overallValue)) {
            dataList = articleService.findOpArticleList(params);
        }
        model.addAttribute("dataList", dataList);
        return resultPage("/globalSearch/groom-search-article-list");
    }
}
